package com.cosmic.pizza.service.Impl;

import com.cosmic.pizza.dao.Impl.OrderDaoImpl;
import com.cosmic.pizza.dao.OrderDao;
import com.cosmic.pizza.pojo.Food;
import com.cosmic.pizza.pojo.Order;
import com.cosmic.pizza.pojo.OrderInfo;
import com.cosmic.pizza.pojo.PageBean;
import com.cosmic.pizza.service.AddressService;
import com.cosmic.pizza.service.FoodService;
import com.cosmic.pizza.service.OrderService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderServiceImpl implements OrderService {
    OrderDao orderDao = new OrderDaoImpl();
    FoodService foodService = new FoodServiceImpl();
    AddressService addressService = new AddressServiceImpl();
    @Override
    public PageBean<Order> findAllOrder(int uid,int currentPage) {
        PageBean<Order> pageBean = new PageBean<>();
        pageBean.setCurrentPage(currentPage);
        int pageSize = 6;
        pageBean.setPageSize(pageSize);
        //查找总数
        int totalCount = orderDao.countByUid(uid);
        pageBean.setTotalCount(totalCount);
        //查询页数
        int totalPage = (totalCount + pageSize - 1)/pageSize;
        pageBean.setTotalPage(totalPage);
        //查询页面食物信息
        int start = (currentPage - 1)*pageSize;
        //根据uid查找所有Order
        List<Order> orders = orderDao.findAllOrder(uid,pageSize,start);
        for (Order order : orders){
            String oid = order.getOid();
            List<OrderInfo> orderInfos = orderDao.findOrderInfoByOid(oid);
            for (OrderInfo orderInfo : orderInfos){
                int fid = orderInfo.getFid();
                //获得food封装
                orderInfo.setFood(foodService.findByFid(fid));
            }
            order.setOrderInfo(orderInfos);
        }
        pageBean.setList(orders);
        return pageBean;
    }

    @Override
    public void CartOrder(Order order, List<OrderInfo> orderInfos) {
        String oid = order.getOid();
        orderDao.insertCartOrder(order);
        for (OrderInfo orderInfo : orderInfos) {
            orderDao.insertOrderInfos(orderInfo);
        }
    }

    @Override
    public void checkOutOrder(String oid, Integer add_id) {
        Map<String,String> map = addressService.findAddressAndNameById(add_id);
        String address = map.get("address");
        String name = map.get("name");
        orderDao.checkOutOrder(oid,address,name);
    }

    @Override
    public boolean PayOrder(String oid) {
        int i = orderDao.PayOrder(oid);
        return i==1;
    }

    @Override
    public boolean GetFlagOrder(String oid) {
        int i = orderDao.GetFlagOrder(oid);
        return i==1;
    }

    @Override
    public List<Order> findNoneGetOrder(int uid) {
        List<Order> list = orderDao.findNoneGetOrder(uid);
        for (Order order : list){
            String oid = order.getOid();
            List<OrderInfo> orderInfos = orderDao.findOrderInfoByOid(oid);
            for (OrderInfo orderInfo : orderInfos){
                int fid = orderInfo.getFid();
                //获得food封装
                orderInfo.setFood(foodService.findByFid(fid));
            }
            order.setOrderInfo(orderInfos);
            }
        return list;
    }

    @Override
    public List<Order> findNonePayOrder(int uid) {
        List<Order> list = orderDao.findNoneGetOrder(uid);
        for (Order order : list){
            String oid = order.getOid();
            List<OrderInfo> orderInfos = orderDao.findOrderInfoByOid(oid);
            for (OrderInfo orderInfo : orderInfos){
                int fid = orderInfo.getFid();
                //获得food封装
                orderInfo.setFood(foodService.findByFid(fid));
            }
            order.setOrderInfo(orderInfos);
        }
        return list;
    }

    @Override
    public Order findOrderByOid(String oid) {
        Order order = orderDao.findOrderByOid(oid);
        List<OrderInfo> orderInfos = orderDao.findOrderInfoByOid(oid);
        for (OrderInfo orderInfo : orderInfos){
            int fid = orderInfo.getFid();
            //获得food封装
            orderInfo.setFood(foodService.findByFid(fid));
        }
        order.setOrderInfo(orderInfos);
        return order;
    }

    @Override
    public void eval(String oid, String fid, String size, String evalMes) {
        int ffid = Integer.parseInt(fid);
        orderDao.eval( oid, ffid, size, evalMes);
        Food food = foodService.findByFid(ffid);
        int sum = Integer.parseInt(food.getSum()) + Integer.parseInt(evalMes);
        int time = Integer.parseInt(food.getEvalTime())+1;
        int evalsum = sum/time;
        foodService.updateEval(ffid,String.valueOf(time),evalsum,String.valueOf(sum));
    }

    @Override
    public boolean ifEval(String oid, String fid, String size) {
        OrderInfo orderInfo = orderDao.findOrderInfo(oid,Integer.parseInt(fid),size);
        return orderInfo.getEvalFlag().equals("null");
    }
}
